// import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const app = getApp()
var common = require("../../utils/cityArray");
var arrays = common.getAreaInfo();
Page({
  data: {
    contactName: '',
    index: ['电脑维护', '监控','服务器','办公设备'],
    locat:"",
    faultExpression: '',
    faultName: '',
    citysIndex: [0, 0, 0], //给一个初始值索引，因为有三列，所以3个0
    address:"",//详细信息的值
    a: '',
    // 时间
    minHour: 0,
    maxHour: 24,
    minDate: new Date(1990,1,1).getTime(),
    maxDate: new Date(2099, 12, 31).getTime(),
    currentDate: new Date().getTime(),
    show: false,
    reserveTime: '',
        // 错误提示
        phoneLength: false,
        phoneFormat: false
  },
  // 时间选择器
  openPicker() {
    this.setData({ show: true })
  },
  onConfirm(e) {
    this.setData({ show: false, reserveTime: this.formatDate(new Date(e.detail)) })
  },
  onClose() {
    this.setData({ show: false })
  },
  onCancel() {
    this.setData({ show: false })
  },
  formatDate(date) {
    let taskStartTime
    if (date.getMonth() < 9) {
      taskStartTime = date.getFullYear() + "-0" + (date.getMonth() + 1) + "-"
    } else {
      taskStartTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
    }
    if (date.getDate() < 10) {
      taskStartTime += "0" + date.getDate()
    } else {
      taskStartTime += date.getDate()
    }
    taskStartTime += " " + date.getHours() + ":" + date.getMinutes()
    this.setData({
      taskStartTime: taskStartTime,
    })
    return taskStartTime;
  },

  //获取维修描述的值
  textareaFault:function(e){
    this.setData({
      faultExpression:e.detail.value
    })
  },
  // 获取故障类型的值
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带为', e.detail.value);
    console.log(this.data.index[e.detail.value])
    this.setData({
        faultName: e.detail.value,
        a :this.data.index[e.detail.value]
    })
  },
  // 获取性别的值
  bindSexChange: function (e) {
    console.log('picker发送选择改变，携带为', e.detail.value);
    this.setData({
        index1: e.detail.value
    })
  },
  // 获取日期的值
  bindDateChange: function (e) {
      this.setData({
        reserveTime: e.detail.value
      })
  },

    /**
   * 输入手机号
   */
  setphoneNumber: function (event) {
    // console.log('username', event.detail.value)
    this.setData({
      contactPhone: event.detail.value
    })
    const regex = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
    if (this.data.contactPhone.length !== 0 && this.data.contactPhone.length !== 11) {
      wx.showToast({
        title: "手机号长度为11位",
        icon: 'none',
        duration: 2000
      })
    } else if (this.data.contactPhone.length !== 0 && !regex .test(this.data.contactPhone)) {
      wx.showToast({
        title: "手机号格式不对",
        icon: 'none',
        duration: 2000
      })
    }
  },


  // 提交订单
  submitForm: function () {
    const value = wx.getStorageSync('token');
    var that = this;
    if(this.data.contactName == ''||this.data.contactPhone.length == 0||this.data.a == ''||this.data.locat==''||this.data.address==''||this.data.reserveTime==''||this.data.faultExpression=='') {
      wx.showToast({
        title: '需填写全部填写项',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: app.globalData.baseurl+'/orderForm/addCOrderForm',
        method: 'post',
        data: {
          contactName: that.data.contactName,
          contactPhone: that.data.contactPhone,
          faultName: that.data.a,
          location: that.data.locat,
          address: that.data.address,
          reserveTime1: that.data.reserveTime,
          faultExpression: that.data.faultExpression,
        },
        header: {
          'content-type': "application/json",
          'Authorization':value,
        },
        success: function(res) {
          console.log(res);
          if (res.data.code == 200) {
            wx.showToast({
              title: '申报成功！',
              icon: 'none',
              duration: 2000
            });
            wx.reLaunch({
              url: '/pages/orderform/orderform'
            })
          } else if(res.data.code === 500) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail: function (res) {
          console.log('提交失败');
          wx.showToast({
            title: "提交失败！",
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  },
  onLoad: function (options) {
    var that = this;
    if (wx.getStorageSync('global_cityData')) {
      var cityArray = wx.getStorageSync('global_cityData');
    } else {
      //定义三列空数组
      var cityArray = [
        [],
        [],
        [],
      ];
      for (let i = 0, len = arrays.length; i < len; i++) {
        switch (arrays[i]['level']) {
          case 1:
            //第一列
            cityArray[0].push(arrays[i]["name"]);
            break;
          case 2:
            //第二列(默认由第一列第一个关联)
            if (arrays[i]['sheng'] == arrays[0]['sheng']) {
              cityArray[1].push(arrays[i]["name"]);
            }
            break;
          case 3:
            //第三列(默认第一列第一个、第二列第一个关联)
            if (arrays[i]['sheng'] == arrays[0]['sheng'] && arrays[i]['di'] == arrays[1]['di']) {
              cityArray[2].push(arrays[i]["name"]);
            }
            break;
        }
      }
      wx.setStorageSync('global_cityData', cityArray);
    }

    that.setData({
      cityArray: cityArray
    });
  },
  func_changeCitysChange: function(e) {
    var that = this;
    var cityArray = that.data.cityArray;

    var address='';
    if (that.data.ssq == undefined){
      //下面方法中没有设置ssq，应该给它默认值 ，此时citysIndex相当于[0,0,0]
      var citysIndex = that.data.citysIndex;
      for (let i in citysIndex) {
        address += cityArray[i][citysIndex[i]]
      }
    }else{
      address = that.data.ssq;
    }
    that.setData({
      locat:address
    })
  },
  func_changeCitysChangeColumn: function(e) {
    var that = this;
    var cityArray = that.data.cityArray;

    var list1 = []; //存放第二列数据，即市的列
    var list2 = []; //存放第三列数据，即区的列
    
    var citysIndex = [];
    //主要是注意地址文件中的字段关系，省、市、区关联的字段有 sheng、di、level
    switch (e.detail.column) {
      case 0:
        //滑动左列
        for (let i = 0, len = arrays.length; i < len; i++) {          
          if (arrays[i]['name'] == cityArray[0][e.detail.value]) {
            var sheng = arrays[i]['sheng'];
          }
          if (arrays[i]['sheng'] == sheng && arrays[i]['level'] == 2) {
            list1.push(arrays[i]['name']);
          }
          if (arrays[i]['sheng'] == sheng && arrays[i]['level'] == 3 && arrays[i]['di'] == arrays[1]['di']) {
            list2.push(arrays[i]['name']);
          }
        }       
        

        citysIndex = [e.detail.value, 0, 0];
        var ssq = cityArray[0][e.detail.value] + list1[0] + list2[0]+'';          

        that.setData({
          global_sheng: sheng
        });  
        break;
      case 1:
        //滑动中列
        var  di;
        // 42 表示的第一个省
        var sheng = that.data.global_sheng?that.data.global_sheng:"42";
        list1 = cityArray[1];
        for (let i = 0, len = arrays.length; i < len; i++) {
          if (arrays[i]['name'] == cityArray[1][e.detail.value]) {
            di = arrays[i]['di'];
          } 
          if (arrays[i]['sheng'] == sheng && arrays[i]['level'] == 3 && arrays[i]['di'] == di) {
            list2.push(arrays[i]['name']);
          }
        }
        citysIndex = [that.data.citysIndex[0], e.detail.value, 0];
        
        var ssq = cityArray[0][that.data.citysIndex[0]] + list1[e.detail.value] + list2[0] + '';

        break;
      case 2:
        //滑动右列
        list1 = cityArray[1];
        list2 = cityArray[2];
        citysIndex = [that.data.citysIndex[0], that.data.citysIndex[1], e.detail.value];

        var ssq = cityArray[0][that.data.citysIndex[0]] + list1[that.data.citysIndex[1]] + list2[e.detail.value] + '';
        break;
    }

    that.setData({
      "cityArray[1]": list1,//重新赋值中列数组，即联动了市
      "cityArray[2]": list2,//重新赋值右列数组，即联动了区
      citysIndex: citysIndex,//更新索引
      ssq: ssq,//获取选中的省市区
    });
  },
})